import Header from '../../components/Header/Header.vue';
import { mapSelectOpions } from '../../lib/utils';
import { Ajax } from '../../lib/ajax';

export default {
    name: 'ProjectEdit',
    components: {
        'ivu-header': Header,
    },
    data() {
        return {
            isLoading: false,
            headerOption: {
                searchBar: {
                    isShow: false,
                },
                isScrolling: false,
            },
            submitLoading: false,
            // 即项目信息
            queryData: {
                bizId: '',
                projName: '',
                feCode: '',
                beCode: '',
                moduleList: [],
            },
            bizList: [],
            fieldsetTitle: '添加新项目',
            projId: this.$route.params.projId || this.$route.query.projId,
        };
    },
    created() {
        if (this.projId) {
            this.fieldsetTitle = '修改项目';
            this.ajaxGetProjectDetail();
        }
        this.handleScroll();
        this.ajaxGetBizList();
    },
    computed: {
        bizSelectOptions() {
            return mapSelectOpions(this.bizList, 'bizId', 'bizName');
        },
    },
    methods: {
        handleScroll() {
            window.addEventListener('scroll', () => {
                this.headerOption.isScrolling = document.documentElement.scrollTop > 20;
            });
        },
        ajaxGetBizList() {
            this.isLoading = true;
            // this.Ajax({
            //     url: '/api/biz/list',
            // })
            // .then((response) => {
            //     const res = response.data;
            //     this.bizList = res.data.bizList;
            //     this.isLoading = false;
            // });
            Ajax.get({
                url: '/api/biz/list',
                success: (res) => {
                    if (res.code.toString() === '200') {
                        this.bizList = res.data;
                        this.isLoading = false;
                    } else {
                        this.isLoading = false;
                    }
                },
                error: () => {
                    this.isLoading = false;
                }
            });
        },

        /**
         * ajaxGetProjectDetail 获取要修改的项目详情
         */
        ajaxGetProjectDetail() {
            this.isLoading = true;
            this.Ajax({
                url: '/api/project/detail',
                params: {
                    projId: this.projId,
                },
            })
            .then((res) => {
                this.queryData = res.data.data;
                this.isLoading = false;
            });
        },

        /**
         * ajaxEditProject 请求ajax接口编辑项目
         * @param  {string} projId 项目id
         */
        ajaxEditProject(projId) {
            this.submitLoading = true;
            let bizName = null;
            this.bizList.some(item => {
                if (item.bizId === this.queryData.bizId) {
                    bizName = item.bizName;
                }
            });

            // if (projId) {
            //     ajaxOption.data.projId = projId;
            // }
            Ajax.post({
                url: '/api/project/edit',
                params: {
                    bizId: this.queryData.bizId,
                    projName: this.queryData.projName,
                    feCode: this.queryData.feCode,
                    beCode: this.queryData.beCode,
                    bizName,
                    projId: `${new Date().getTime()}`,
                },
                success: (res) => {
                    if (res.code.toString() === '200') {
                        this.submitLoading = false;
                        this.$Message.success({
                            content: '操作成功',
                            onClose: () => {
                                this.$router.push({
                                    path: '/',
                                });
                            },
                        });
                    } else {
                        this.submitLoading = false;
                    }
                },
                error: () => {
                    this.submitLoading = false;
                }
            });
        },

        /**
         * submitEditProject 提交编辑项目
         * @param  {Boolean} isSubmit 是否确认编辑
         */
        submitEditProject(isSubmit) {
            if (!isSubmit) {
                this.$router.push({
                    path: '/',
                });
                return;
            }

            const queryData = this.queryData;
            if (!queryData.bizId ||
                !queryData.projName ||
                !queryData.feCode ||
                !queryData.beCode) {
                this.$Message.error({
                    content: '请完善项目信息',
                });
                return;
            }

            // 校验选择业务线不为空
            this.ajaxEditProject();
        },
    },
    watch: {
        isLoading(newVal) {
            if (newVal) {
                this.$Spin.show();
            } else {
                this.$Spin.hide();
            }
        },
    },
};
